package com.whccb.apps.office.document.at;

import java.util.List;

import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.TaskInstance;
import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.util.ConsolePrinter;
import com.actionsoft.exception.AWSExpressionException;
import com.actionsoft.sdk.local.SDK;

public class DocCheckAt extends AbstExpression {
    private ExpressionContext at;

    public DocCheckAt(ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
        // TODO Auto-generated constructor stub
        at = atContext;
    }

    @Override
    public String execute(String arg0) throws AWSExpressionException {
        // TODO Auto-generated method stub
        ProcessInstance pi = at.getProcessInstance();
        String result = "";
        String taskdefid = "obj_c7476cfc08700001689b15e01bf393c0";
        List<TaskInstance> tiList = SDK.getTaskQueryAPI()
                .processInstId(pi.getId()).activityDefId(taskdefid)
                .orderByBeginTime().desc().list();
        if (tiList != null && !tiList.isEmpty() && tiList.size() > 0) {
            ConsolePrinter.info("查询节点实例数------" + tiList.size());
            ConsolePrinter.info("查询节点实例ID-----" + tiList.get(0).getId());
            if (SDK.getTaskAPI().isChoiceActionMenu(tiList.get(0), "定稿发文")) {
                ConsolePrinter.info("审批结果------定稿发文");
                result = "1";
            }
            if (SDK.getTaskAPI().isChoiceActionMenu(tiList.get(0), "退回拟稿人")) {
                ConsolePrinter.info("审批结果------退回拟稿人");
                result = "2";
            }
            if (SDK.getTaskAPI().isChoiceActionMenu(tiList.get(0), "作废")) {
                ConsolePrinter.info("审批结果------作废");
                result = "3";
            }
        }
        // if (tiList != null && !tiList.isEmpty() && tiList.size() > 0) {
        // String sql = "SELECT ACTIONNAME FROM WFC_COMMENT WHERE PROCESSINSTID
        // =:PROCESSINSTID AND TASKINSTID =:TASKINSTID AND ACTIONNAME<>'激活' AND
        // ROWNUM=1 ";
        // Map<String, Object> deptmap = new HashMap<String, Object>();
        // deptmap.put("PROCESSINSTID", pi.getId());
        // deptmap.put("TASKINSTID", tiList.get(0).getId());
        // String actionname = DBSql.getString(sql, "ACTIONNAME", deptmap);
        // if ("定稿发文".equals(actionname)) {
        // result = "1";
        // } else if ("退回拟稿人".equals(actionname)) {
        // result = "2";
        // } else if ("作废".equals(actionname)) {
        // result = "3";
        // }
        // ConsolePrinter.info("路由判断结果act-------" + actionname);
        // }
        return result;
    }

}
